package com.sys.Utils;

import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.context.support.GenericXmlApplicationContext;
import org.springframework.util.StringUtils;

import com.alibaba.fastjson.JSONObject;
import com.business.dao.CarBrandMapper;
import com.business.dao.CarModelMapper;
import com.business.dao.CarSeriesMapper;
import com.business.domain.CarBrand;
import com.business.domain.CarBrandExample;
import com.business.domain.CarBrandExample.Criteria;
import com.business.domain.CarModel;
import com.business.domain.CarModelExample;
import com.business.domain.CarModelVo;
import com.business.domain.CarSeries;
import com.business.domain.CarSeriesExample;
import com.business.domain.CarSeriesVo;

public class InputTest {

	public static void main(String[] args) {
		GenericXmlApplicationContext context = new GenericXmlApplicationContext();
		context.setValidating(false);
		context.load("classpath*:spring-mybatis.xml");
		context.refresh();
		CarBrandMapper carBrandMapper = (CarBrandMapper) context.getBean("carBrandMapper");
		CarSeriesMapper carSeriesMapper = (CarSeriesMapper) context.getBean("carSeriesMapper");
		CarModelMapper carModelMapper = (CarModelMapper) context.getBean("carModelMapper");

		String fileName = "E:\\model\\n";
		File file = new File(fileName);
		File[] listFiles = file.listFiles();
		int total = 0;
		int j = 0;
		try {
			for (File file2 : listFiles) {
				Long bentime = System.currentTimeMillis();
				System.out.println("路径：-->" + fileName + "\\" + file2.getName());
				List<JSONObject> inputData = InputUtils.inputData(fileName + "\\" + file2.getName());
				total += inputData.size();
				Long endtime = System.currentTimeMillis();
				System.out.println("读取文件耗时：" + (endtime - bentime) + "数据量：" + inputData.size());
				List<CarModel> mList = new ArrayList<CarModel>();
				// 需要导入 品牌
				CarBrand brand = new CarBrand();
				brand.setCreatime(new Date());
				brand.setName(inputData.get(0).getString("brandname"));
				CarBrandExample brandExample = new CarBrandExample();
				Criteria createBrand = brandExample.createCriteria();
				createBrand.andNameEqualTo(brand.getName());
				List<CarBrand> listBrand = carBrandMapper.selectByExample(brandExample);
				if (listBrand != null && listBrand.size() > 0) {
					brand.setId(listBrand.get(0).getId());
				} else {
					carBrandMapper.insertSelective(brand);
					// System.out.println(brand.toString());
				}
				//需要导入车系
				CarSeries series = new CarSeries();
				series.setCreatime(new Date());
				series.setName(inputData.get(0).getString("seriesname"));
				series.setBrandId(brand.getId());
				CarSeriesExample seriesExample = new CarSeriesExample();
				com.business.domain.CarSeriesExample.Criteria createSeries = seriesExample.createCriteria();
				createSeries.andNameEqualTo(series.getName());
				List<CarSeriesVo> listSeries = carSeriesMapper.selectByExample(seriesExample);
				if (listSeries != null && listSeries.size() > 0) {
					series.setId(listSeries.get(0).getId());
				} else {
					carSeriesMapper.insertSelective(series);
				}
				for (JSONObject obj : inputData) {

					CarModel model = new CarModel();
					model.setCreatime(new Date());
					model.setSeriesId(series.getId());
					model.setName(obj.getString("specname"));
					String guidePrice = obj.getString("specmaxprice");
					model.setGuidePrice(BigDecimal.valueOf(Double.valueOf(StringUtils.isEmpty(guidePrice) ? "0" : guidePrice)));
					model.setLevelName(obj.getString("levelname"));
					model.setEngineModename(obj.getString("specid"));

					guidePrice = obj.getString("specdisplacement");
					model.setDisplacement(Double.valueOf(StringUtils.isEmpty(guidePrice) ? "0" : guidePrice));

					guidePrice = obj.getString("specenginepower");
					model.setHorsepower(Double.valueOf(StringUtils.isEmpty(guidePrice) ? "0" : guidePrice));
					model.setFlowModename(obj.getString("specflowmodename"));
					model.setDrivingModename(obj.getString("specdrivingmodename"));

					// guidePrice =obj.getString("specpicount");
					// model.setMaxSped(Double.valueOf(StringUtils.isEmpty(guidePrice)?"0":guidePrice));

					guidePrice = obj.getString("specoiloffical");
					model.setOilConsumption(Double.valueOf(StringUtils.isEmpty(guidePrice) ? "0" : guidePrice));

					// guidePrice =obj.getString("spectransmission");
					// model.setGearboxNum(Double.valueOf((StringUtils.isEmpty(guidePrice)?"0":guidePrice).substring(0,
					// 1)).intValue());
					model.setTransmissionType(obj.getString("spectransmission"));

					guidePrice = obj.getString("specheight");
					model.setHeight(Double.valueOf(StringUtils.isEmpty(guidePrice) ? "0" : guidePrice).intValue());

					guidePrice = obj.getString("speclength");
					model.setLength(Double.valueOf(StringUtils.isEmpty(guidePrice) ? "0" : guidePrice).intValue());

					guidePrice = obj.getString("specwidth");
					model.setWidth(Double.valueOf(StringUtils.isEmpty(guidePrice) ? "0" : guidePrice).intValue());

					guidePrice = obj.getString("specweight");
					model.setWeight(Double.valueOf(StringUtils.isEmpty(guidePrice) ? "0" : guidePrice).intValue());
					// model.setWheelBase(Double.valueOf(obj.getString("seriesname")).intValue());
					model.setQuality(obj.getString("specquality"));

					guidePrice = obj.getString("specstructuredoor");
					model.setStrucDoor(Double.valueOf(StringUtils.isEmpty(guidePrice) ? "0" : guidePrice).intValue());

					guidePrice = obj.getString("specstructureseat");
					/*
					 * int i = 0; if(!StringUtils.isEmpty(guidePrice) &&
					 * (i=guidePrice.indexOf("-"))!=-1){ guidePrice =
					 * guidePrice.substring(i+1, guidePrice.length()); }
					 */
					// model.setTrucSeat(Double.valueOf(StringUtils.isEmpty(guidePrice)?"0":guidePrice).intValue());
					// model.setStrucTypename(Integer.valueOf(row.getCell(23).toString()));

					CarModelExample carModelExample = new CarModelExample();
					com.business.domain.CarModelExample.Criteria createModel = carModelExample.createCriteria();
					createModel.andNameEqualTo(model.getName());
					createModel.andEngineModenameEqualTo(model.getEngineModename());
					List<CarModelVo> listModel = carModelMapper.selectByExample(carModelExample);
					if (listModel != null && listModel.size() > 0) {
						series.setId(model.getId());
						System.out.println("品牌：" + obj.getString("brandname") + "：车系：" + obj.getString("seriesname") + "：车型：" + obj.getString("specname") + "：指导价：" + obj.getString("specminprice"));
						System.out.println("已经导入！跳过");
					} else {
						mList.add(model);
						j++;
						System.out.println(j + ":--》品牌：" + obj.getString("brandname") + "：车系：" + obj.getString("seriesname") + "：车型：" + obj.getString("specname") + "：指导价：" + obj.getString("specminprice"));
					}
				}
				if (mList != null && mList.size() > 0) {
					int list = carModelMapper.insertCarModelBatch(mList);
					if (list > 0) {
						Long lasttime = System.currentTimeMillis();
						System.out.println("导入数据库耗时：" + (lasttime - endtime));
					} else {
						System.out.println("导入失败!");
					}
				}
			}
			System.out.println("共：" + total + " 成功：" + j + " 失败：" + (total - j));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
